<?php
class lib_db_mysql{
	
	private $con;
	
	//连接数据库
	function __construct(){
		$this->con = mysql_connect(m("#config")->dbHost,m("#config")->dbUser,m("#config")->dbPass);
		if(!$this->con){
			die("连接数据库失败");
		}
		mysql_query("SET NAMES 'utf8'");
		mysql_select_db(m("#config")->dbName);
	}
	
	function __destruct(){
		mysql_close($this->con);
	}
	/**
	 * 获取查询结果
	 * @param $query
	 */
	function get($queryArr){
		/**
		 * SELECT
		 */
		if(is_array($queryArr['select'])){
			foreach ($queryArr['select'] as $key => $field){
				$select .= $key.' AS '.$field.',';
			}
			$select = trim($select,',');
		}else{
			$select = '*';
		}
			
		$queryStr = 'SELECT '.$select;		
		
		/**
		 * FROM
		 */
		
		$queryStr .= ' FROM '.$queryArr['table'];
		/**
		 * WHERE
		 */
		if(is_array($queryArr['find'])){
			$queryStr .= ' WHERE '.$this->where($queryArr['find']);
		}
		
		/**
		 * ORDERBY
		 */
		if(is_array($queryArr['order'])){
			$order = '`'.$queryArr['order'][0].'`'.((empty($queryArr['order'][1]))?' ASC ':' DESC ');
			$queryStr .= ' ORDER BY '.$order;
		}
		
		
		/**
		 * LIMIT
		 */
		if(is_array($queryArr['limit'])){
			$queryStr .= ' LIMIT '.$queryArr['limit'][0].','.$queryArr['limit'][1];
		}
		
//		echo $queryStr.'<br>';

		/**
		 * 查询并返回结果
		 */
		m()->debug('s',$queryStr);
		$result = mysql_query($queryStr,$this->con);
		if(!empty($result))
		while($rsarray[]=mysql_fetch_assoc($result));
		return array_filter($rsarray);
	}
	
	function count($queryArr){
		$queryArr['select']=NULL;
		$queryArr['select']['COUNT(1)']='count';
		$rs = $this->get($queryArr);
		return (int)$rs[0]['count'];
	}
	
	/**
	 * 添加
	 * @param array $query
	 */
	function add($queryArr){
		if(empty($queryArr['set']))
			return FALSE;
		$queryStr = 'INSERT INTO '.$queryArr['table'].' '.$this->set($queryArr['set']);
		/**
		 * 查询并返回结果
		 */
		m()->debug('SQL',$queryStr);
		$result = mysql_query($queryStr,$this->con);
		if($result)
			return mysql_insert_id();
		return FALSE;
	}
	
	/**
	 * 编辑
	 * @param array $query
	 */
	function save($queryArr){
		if(empty($queryArr['set']))
			return FALSE;
		
		$queryStr = 'UPDATE '.$queryArr['table'].' '.$this->set($queryArr['set']).' WHERE '.$this->where($queryArr['find']);
		
		/**
		 * 查询并返回结果
		 */
		m()->debug('SQL',$queryStr);
//		echo $queryStr;
		$result = mysql_query($queryStr,$this->con);
		if(!$result)
			return FALSE;
		return TURE;
	}
	
	/**
	 * 删除
	 * @param array $query
	 */
	function del($queryArr){
		//DELETE FROM `per`.`user` WHERE `user`.`id` = 4 LIMIT 1
		$queryStr = 'DELETE FROM '.$queryArr['table'].' WHERE '.$this->where($queryArr['find']);

		/**
		 * 查询并返回结果
		 */
		m()->debug('SQL',$queryStr);
		$result = mysql_query($queryStr,$this->con);
		if(!$result)
			return FALSE;
		return TURE;
	}
	
	private function where($arr){
		/**
		 * WHERE
		 */
		if(is_array($arr)){
			foreach ($arr as $find){
				if(empty($find[4])){
					$where .= $find[0].' `'.$find[1].'` '.$find[2].' \''.$find[3].'\' ';
				}else{
					$where .= $find[0].' `'.$find[1].'` '.$find[2].' '.$find[3].' ';
				}
			}
			$where = trim($where,'AND ');
		}
		return $where;
	}
	
	private function set($arr){
		$set="SET ";
		foreach($arr as $key => $value){
			if(isset($value))$set .="`$key`='$value',";
		}
		return rtrim($set,',');
	}
	
}